#include "ifconditiondialog.h"
#include "ui_ifconditiondialog.h"
#include <QListWidget>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QDebug>
#include <QModelIndex>
#include <QMessageBox>


IfConditionDialog::IfConditionDialog(int styleId, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IfConditionDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setStyleSheet("background-color:white;");
    //    setGeometry(70,128,460,595);
    move(0,0);
    initialDisplayStyle(styleId);

    ui->tableView->setShowGrid(true);
    ui->tableView->setGridStyle(Qt::DotLine);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    model = new QStandardItemModel();
    QStringList labels = QObject::trUtf8("条件模板").simplified().split(",");
    model->setHorizontalHeaderLabels(labels);
    QStringList templateList;
    templateList.append("PL[1]=[1,1,1]");
    templateList.append("PL[1]<>[1,1,1]");
    templateList.append("PL[1]=PL[2]");
    templateList.append("PL[1]<>PL[2]");
    templateList.append("R[1]>1");
    templateList.append("R[1]>=1");
    templateList.append("R[1]=1");
    templateList.append("R[1]<1");
    templateList.append("R[1]<=1");
    templateList.append("R[1]<>1");
    templateList.append("R[1]=R[2]");
    templateList.append("AO[1]>1");
    templateList.append("AI[1]>1");
    templateList.append("DI[1]<>ON");
    templateList.append("DI[1]<>OFF");
    templateList.append("DI[1]=ON");
    templateList.append("DI[1]=OFF");
    templateList.append("DI[1]=DI[2]");
    templateList.append("DO[1]<>ON");
    templateList.append("DO[1]<>OFF");
    templateList.append("DO[1]=ON");
    templateList.append("DO[1]=OFF");
    setTemplateList(templateList);


}

IfConditionDialog::~IfConditionDialog()
{
    delete ui;
}

void IfConditionDialog::initialDisplayStyle(int styleId)
{
//    switch (styleId) {
//    case 1:
//        ui->tableView->setGeometry(10,20,420,320);
//        ui->back_Btn->setGeometry(360,355,70,40);
//        break;
//    default:
//        break;
//    }
}


void IfConditionDialog::setTemplateList(QStringList fileList)
{
    QStandardItem* item = 0;
    for(int i = 0;i < fileList.length();i++){
       item = new QStandardItem(QString("%1").arg(fileList.at(i)));
       model->setItem(i,0,item);
       //item->setTextAlignment(Qt::AlignCenter);
    }
    ui->tableView->setModel(model);
    ui->tableView->show();
}

void IfConditionDialog::on_back_Btn_clicked()
{
    this->hide();
}

void IfConditionDialog::on_button_select_clicked()
{
    int row= ui->tableView->currentIndex().row();
    if(row>=0)
    {
        QAbstractItemModel *model = ui->tableView->model ();
        QModelIndex index = model->index(row,0);//选中行第一列的内容
        QString cellText = index.data().toString();
        qDebug()<< "str:" << cellText;
        emit sendCondition_signal(cellText);
        this->hide();
    }
    else
    {
        QMessageBox::information(this, "提示", "没有选中行！", QMessageBox::Yes );
    }
}
